package io.renren.modules.sys.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.toolkit.StringUtils;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.frt.entity.DistrictEntity;
import io.renren.modules.frt.service.DistrictService;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserService;

/**
 * 系统用户
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年10月31日 上午10:40:10
 */
@RestController
@RequestMapping("/sys/user2")
public class SysUserController2 extends AbstractController {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private DistrictService districtService;
	
	/**
	 * 所有用户列表
	 */
	@SuppressWarnings("unchecked")
	@GetMapping("/list")
	@RequiresPermissions("sys:user:list")
	public R list(@RequestParam Map<String, Object> params){
		//只有超级管理员，才能查看所有管理员列表
		PageUtils page = sysUserService.queryPage(params);
		
		List<SysUserEntity> list = (List<SysUserEntity>) page.getList();
		if(list != null && list.size() > 0) {
			for(SysUserEntity user : list) {
				if(StringUtils.isNotEmpty(user.getProvince())) {
					DistrictEntity province = districtService.selectOne(new EntityWrapper<DistrictEntity>().eq("code", user.getProvince()));
					user.setProvinceName(province != null ? province.getName() : "");
				}
				if(StringUtils.isNotEmpty(user.getCity())) {
					DistrictEntity city = districtService.selectOne(new EntityWrapper<DistrictEntity>().eq("code", user.getCity()));
					user.setCityName(city != null ? city.getName() : "");
				}
				if(StringUtils.isNotEmpty(user.getCountry())) {
					DistrictEntity country = districtService.selectOne(new EntityWrapper<DistrictEntity>().eq("code", user.getCountry()));
					user.setCountryName(country != null ? country.getName() : "");
				}
			}
		}
		return R.ok().put("page", page);
	}
}
